package com.yl.order.ui.dialog;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yl.common.ui.BaseDialogFragment;
import com.yl.order.R;
import com.yl.order.interfaces.Callback;
import com.yl.order.interfaces.ConfirmCallback;

import java.io.IOException;
import java.net.InetAddress;
import java.util.regex.Pattern;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * create by cjx on 2024/5/16
 * desc: 配置出料选项和补料选项
 */
public class EditDialogFragment extends BaseDialogFragment implements View.OnClickListener {

    private ConfirmCallback<String> confirmCallback;
    private String title;
    private String value;
    private String hint;
    private EditText editText;

    public static EditDialogFragment newInstance() {
        return new EditDialogFragment();
    }

    @Override
    public int getViewId() {
        return R.layout.dialog_edit;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!TextUtils.isEmpty(title)) {
            ((TextView) findViewById(R.id.title)).setText(title);
        }
        editText = findViewById(R.id.edit);
        editText.setHint(hint);
        editText.setText(value);
        findViewById(R.id.close).setOnClickListener(this);
        findViewById(R.id.confirm).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.confirm) {
            String ip = editText.getText().toString();
            String pattern = "((2(5[0-5]|[0-4]\\d))|((1\\d{2})|[1-9]\\d|[0-9]))(\\.((2(5[0-5]|[0-4]\\d))|((1\\d{1,2})|[1-9]\\d|[0-9]))){3}";
            boolean matches = Pattern.matches(pattern, ip);
            if (!matches) {
                showToast("请输入正确的ip地址");
                return;
            }
            check(ip, success -> {
                if (success) {
                    if (confirmCallback.confirm(editText.getText().toString())) {
                        dismiss();
                    }
                } else {
                    showToast(ip+"和本机不在同一个网段！");
                }
            });
        } else {
            dismiss();
        }
    }

    private void check(String url, Callback<Boolean> callback) {
        LoadingDialogFragment loadingDialog = LoadingDialogFragment.newInstance().setTitle(title);
        loadingDialog.setCanceledOnTouchOutside(false)
                .show(getFragmentManager(), "loading");
        Schedulers.io().scheduleDirect(() -> {
            boolean success;
            try {
                success = InetAddress.getByName(url).isReachable(2000);
            } catch (IOException e) {
                success = false;
            }
            final boolean result = success;
            AndroidSchedulers.mainThread().scheduleDirect(() -> {
                loadingDialog.dismiss();
                callback.callback(result);
            });
        });
    }

    public EditDialogFragment setConfirmCallback(ConfirmCallback<String> confirmCallback) {
        this.confirmCallback = confirmCallback;
        return this;
    }

    public EditDialogFragment setTitle(String title) {
        this.title = title;
        return this;
    }

    public EditDialogFragment setValue(String value) {
        this.value = value;
        return this;
    }

    public EditDialogFragment setHint(String hint) {
        this.hint = hint;
        return this;
    }
}
